﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ClassLibrary.DAO;

public partial class Admin_ucControl_ucListOrder : System.Web.UI.UserControl
{
    TblBookingDAO bookingDAO = new TblBookingDAO(Global.CONNECTION_STRING);

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session[Global.SESSION_ADMIN_USER] == null)
            {
                Response.Redirect("~/Admin/Login.aspx");
            }
            LoadListOrder();
        }
    }

    private void LoadListOrder()
    {
        try
        {
            lvItem.DataSource = bookingDAO.GetOrderLst(Global.ConvertToDate(txtFrDate.Text), Global.ConvertToDate(txtToDate.Text), Global.ConvertToInt(DropDownList1.SelectedValue.ToString()));
            lvItem.DataBind();
        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect(Global.ERROR_PAGE_ADMIN);
        }
    }

    protected void btnSubmit_Click1(object sender, ImageClickEventArgs e)
    {
        LoadListOrder();
    }
    protected void lvItem_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        if (e.Item.ItemType == ListViewItemType.DataItem)
        {

            ListViewDataItem dataItem = (ListViewDataItem)e.Item;
            string hid = DataBinder.Eval(dataItem.DataItem, "BookID").ToString();
            HiddenField hid1 = (HiddenField)e.Item.FindControl("hid");
            hid1.Value = hid;

            DataTable Order = new DataTable();
            TblBookingDAO orderDAO = new TblBookingDAO(Global.CONNECTION_STRING);
            Order = orderDAO.GetOrder(Convert.ToInt32(hid));
            DropDownList drStatus = (DropDownList)e.Item.FindControl("drStatus");

            Label lbMethod = (Label)e.Item.FindControl("lbphuongthucthanhtoan");
            

            foreach (DataRow dr in Order.Rows)
            {
                if (dr["Status"].ToString() == "0")
                {
                    drStatus.SelectedItem.Text = "Đặt hàng";

                }
                if (dr["Status"].ToString() == "1")
                {
                    drStatus.SelectedItem.Text = "Đã giao hang";

                }
                if (dr["Status"].ToString() == "2")
                {
                    drStatus.SelectedItem.Text = "Huỷ đơn hàng";
                }
                if (dr["Status"].ToString() == "3")
                {
                    drStatus.SelectedItem.Text = "Đã chuyển tiền";
                }

                if (dr["PaymentMethod"].ToString() == "1")
                {
                    lbMethod.Text = "Thanh toán sau";
                }
                if (dr["PaymentMethod"].ToString() == "2")
                {
                    lbMethod.Text = "Chuyển khoản";
                }
            }
        }
    }
    protected void lvItem_PagePropertiesChanged(object sender, EventArgs e)
    {
        lvItem.DataBind();
    }
    protected void drStatus_SelectedIndexChanged(object sender, EventArgs e)
    {
        string ItemID = "";
        foreach (ListViewItem lst in lvItem.Items)
        {
            DropDownList drStatus = (DropDownList)lst.FindControl("drStatus");
            HiddenField hid = (HiddenField)lst.FindControl("hid");
            ItemID = hid.Value;
            DataTable dt = null;

            if (drStatus.SelectedValue == "0")
            {
                dt = bookingDAO.Update(0, Global.ConvertToInt(ItemID));
            }
            if (drStatus.SelectedValue == "1")
            {
                dt = bookingDAO.Update(1, Global.ConvertToInt(ItemID));
            }
            if (drStatus.SelectedValue == "2")
            {
                dt = bookingDAO.Update(2, Global.ConvertToInt(ItemID));
            }
            if (drStatus.SelectedValue == "3")
            {
                dt = bookingDAO.Update(3, Global.ConvertToInt(ItemID));
            }
        }

    }
}
